package com.yanggeng.demo.beginnerguidedemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment2;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment3;
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment4;

import java.util.ArrayList;
import java.util.List;

import static android.support.design.widget.BottomSheetBehavior.STATE_COLLAPSED;
import static android.support.design.widget.BottomSheetBehavior.STATE_EXPANDED;

/**
 * TabLayout+ViewPager做底部导航栏样式主界面
 * Created by yanggeng on 2017/7/20.
 */

public class TabLayoutActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    List<Fragment> fragments;
    String[] titles = {"首页", "商城", "发现", "我的"};
    MainViewPagerAdapter adapter;

    TabLayout.Tab home, mall, find, mine;

    private LinearLayout tabTest;

    public BottomSheetBehavior bottomSheetBehavior;
    private GestureDetector gestureDetector;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kotlin_test);
        tabLayout = (TabLayout) findViewById(R.id.tab_main);
        viewPager = (ViewPager) findViewById(R.id.vp_main);
        tabTest = (LinearLayout) findViewById(R.id.tab_layout);

        initView();
//        bottomSheetBehavior = BottomSheetBehavior.from(tabTest);
////        gestureDetector = new GestureDetector(this, this);
//        bottomSheetBehavior.setPeekHeight(0);
//        bottomSheetBehavior.setState(STATE_COLLAPSED);

    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        return gestureDetector.onTouchEvent(event);
//    }

    @Deprecated
    private void initEvent() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab == tabLayout.getTabAt(0)) {
                    home.setIcon(R.drawable.shape_circle);
                    viewPager.setCurrentItem(0);
                } else if(tab == tabLayout.getTabAt(1)) {
                    mall.setIcon(R.drawable.shape_circle);
                    viewPager.setCurrentItem(1);
                } else if(tab == tabLayout.getTabAt(2)) {
                    find.setIcon(R.drawable.shape_circle);
                    viewPager.setCurrentItem(2);
                } else if(tab == tabLayout.getTabAt(3)) {
                    mine.setIcon(R.drawable.shape_circle);
                    viewPager.setCurrentItem(3);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if(tab == tabLayout.getTabAt(0)) {
                    home.setIcon(R.mipmap.ic_launcher);
                } else if(tab == tabLayout.getTabAt(1)) {
                    mall.setIcon(R.mipmap.ic_launcher);
                } else if(tab == tabLayout.getTabAt(2)) {
                    find.setIcon(R.mipmap.ic_launcher);
                } else if(tab == tabLayout.getTabAt(3)) {
                    mine.setIcon(R.mipmap.ic_launcher);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initView() {
        fragments = new ArrayList<>();
        fragments.add(new TestFragment());
        fragments.add(new TestFragment2());
        fragments.add(new TestFragment3());
        fragments.add(new TestFragment4());

        adapter = new MainViewPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);

        home = tabLayout.getTabAt(0);
        mall = tabLayout.getTabAt(1);
        find = tabLayout.getTabAt(2);
        mine = tabLayout.getTabAt(3);

        home.setIcon(R.drawable.selector_tab_mall_image);
        mall.setIcon(R.drawable.selector_tab_home_image);
        find.setIcon(R.drawable.selector_tab_mall_image);
        mine.setIcon(R.drawable.selector_tab_home_image);
    }

    class MainViewPagerAdapter extends FragmentPagerAdapter {

        public MainViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
